import sqlite3 as sqlite
import re
import time

delim_re = re.compile(r"[: ]")

log_re = re.compile(r"""(?P<item1>\d+):(?P<item2>\d+) (?P<n>\d+).*""")

f = open('temp_db.txt', 'r')

while f.readline()[0] != '-':
    pass

conn = sqlite.connect('temp2.db')
c = conn.cursor()

k = 0
line = f.readline()
items = []
while line:
    if k >= 0:
        if k % 100000 == 0:
            print k
            print 'time = %s' % time.time()
            # Insert a row of data
            c.executemany("insert into items (item1, item2, n) values (?, ?, ?)", items)
            print 'time = %s' % time.time()
            # Save (commit) the changes
            conn.commit()
            print 'time = %s' % time.time()

            items = []

        _l = log_re.match(line)
        l = _l.groupdict()
   
        items.append((l['item1'], l['item2'], l['n']))
   
        #print l
   
    line = f.readline()
    k+=1

f.close()

print k

c.executemany("insert into items (item1, item2, n) values (?, ?, ?)", items)
conn.commit()


# We can also close the cursor if we are done with it
c.close()

